<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="handleAdd(1, '0')">添加权限</el-button>

        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button v-if="row.type === 1" size="small" type="text" @click="handleAdd(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="toEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="handleDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增编辑的弹层 -->
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" :title="formData.id?'编辑权限':'添加权限'" @close="showDialog = false">
      <!-- 表单内容 -->
      <el-form ref="authForm" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="是否展示" prop="enVisible">
          <el-switch v-model="formData.enVisible" active-value="0" inactive-value="1" active-text="启用" inactive-text="禁用" active-color="#13ce66" inactive-color="#eee" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetPermissionList, reqUpdatePermission, reqGetPermissionDetail, reqAddPermission, reqDelPermission } from '@/api/permission'
import { translateListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data () {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      // 表单验证
      rules: {
        name: [
          { required: true, message: '权限点名称不能为空', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '权限点编码不能为空', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.loadPermissionList()
  },
  methods: {
    // 控制关闭弹窗
    handleClose () {
      this.showDialog = false
      this.$refs.authForm.resetFields()
      this.formData = this.$options.data().formData
    },
    // 编辑第一步（回填表单）
    async toEdit (id) {
      const ret = await reqGetPermissionDetail(id)
      this.formData = ret.data
      this.showDialog = true
    },
    // 删除权限点
    handleDelete (id) {
      this.$confirm('确认要删除吗？', '温馨提示').then(async () => {
        const ret = await reqDelPermission(id)
        if (ret.code === 10000) {
          // 删除成功
          this.loadPermissionList()
          this.showDialog = false
        } else {
          this.$message.error(ret.message)
        }
      }).catch((e) => {
        if (e !== 'cancel') {
          // 出错了
          this.$message.error('删除失败')
        }
      })
    },
    // 添加弹窗动作
    handleAdd (type, pid) {
      // 设置权限的级别
      this.formData.type = type
      // 设置权限的父节点
      this.formData.pid = pid
      // 显示弹窗
      this.showDialog = true
    },
    // 提交表单
    // 提交表单
    handleSubmit () {
      this.$refs.authForm.validate(async valid => {
        if (!valid) return
        if (this.formData.id) {
          // 编辑权限
          const ret = await reqUpdatePermission(this.formData)
          if (ret.success) {
            this.loadPermissionList()
            this.handleClose()
          } else {
            this.$message.error(ret.message)
          }
        } else {
          // 添加权限
          const ret = await reqAddPermission(this.formData)
          if (ret.success) {
            // 关闭弹窗
            // this.showDialog = false
            // 刷新列表
            this.loadPermissionList()
            // 清空表单
            // this.$refs.authForm.resetFields()
            // this.formData = this.$options.data().formData
            this.handleClose()
          } else {
            this.$message.error(ret.message)
          }
        }
      })
    },
    // 加载权限列表数据
    async loadPermissionList () {
      try {
        const ret = await reqGetPermissionList()
        if (ret.code === 10000) {
          this.list = translateListToTreeData(ret.data, '0')
          // console.log(this.list)
        } else {
          this.$message.error('获取权限列表失败')
        }
      } catch {
        this.$message.error('获取权限列表失败')
      }
    }
  }
}
</script>
